#include <iostream>
#include <ctime>
#include "class.h"

using namespace std;

bigBox::~bigBox() { }

void bigBox::SetArray(int* d_initArray)
{
	p_initArray = d_initArray;
}

void bigBox::MergeSort()
{
	for (int i = 1; i < 10; i *= 2)
	{
        for (int j = 0; j < (10 - i); j += 2*i)
		{
            int groupEnd2 = (2*i < 10 - j) ? 2*i : 10 - j;
            MergeArray(&(p_initArray[j]), i, groupEnd2);
        }
    }
}

void bigBox::MergeArray(int* arrayPtr, int groupEnd1, int groupEnd2)
{
	int i = 0;
    int j = groupEnd1;
    int k = 0;
    int* temp = new int[groupEnd2];
    // Take each next smallest element
    while (i < groupEnd1 && j < groupEnd2)
	{
        if (arrayPtr[i] < arrayPtr[j])
		{
            temp[k] = arrayPtr[i];
            ++i;
        }
		else
		{
            temp[k] = arrayPtr[j];
            ++j;
        }
        ++k;
    }
    // Copy any remaining elements of the 1st array
    while (i < groupEnd1)
	{
        temp[k] = arrayPtr[i];
        ++i;
        ++k;
    }
    // Copy any remaining elements of the 2nd array
    while (j < groupEnd2)
	{
        temp[k] = arrayPtr[j];
        ++j;
        ++k;
    }
    // Copy the merged array back to the original
    for (int iIndex = 0; iIndex < groupEnd2; ++iIndex)
	{
        arrayPtr[iIndex] = temp[iIndex];
    }
    delete [] temp;

	return;
}

void bigBox::PrintArray()
{
	cout << "\n\nList:\n";
	cout << "------\n";
	for (int i = 0; i < 10; ++i)
	{
		cout << '[' << p_initArray[i] << ']'<< " ";
	}
	cout << "\n\n";
	return;
}